package memento.test;

public class UserInfo {
    private String account;
    private String password;
    private String telNo;

    // 构造函数
    public UserInfo(String account, String password, String telNo) {
        this.account = account;
        this.password = password;
        this.telNo = telNo;
    }

    // === 备忘录内部类（包级私有，更好的封装） ===
    public static class Memento {
        private final String account;
        private final String password;
        private final String telNo;
        private final long timestamp;
        private final String description;

        // 只能通过UserInfo创建
        private Memento(String account, String password, String telNo, String description) {
            this.account = account;
            this.password = password;
            this.telNo = telNo;
            this.timestamp = System.currentTimeMillis();
            this.description = description;
        }

        // 只提供包级访问权限的getter
        String getAccount() {
            return account;
        }

        String getPassword() {
            return password;
        }

        String getTelNo() {
            return telNo;
        }

        public long getTimestamp() {
            return timestamp;
        }

        public String getDescription() {
            return description;
        }

        @Override
        public String toString() {
            return String.format("Memento[%s, %tF %tT]", description, timestamp, timestamp);
        }
    }

    // === 创建备忘录 ===
    public Memento createMemento(String description) {
        return new Memento(account, password, telNo, description);
    }

    // === 恢复状态 ===
    public void restoreFromMemento(Memento memento) {
        if (memento == null) {
            throw new IllegalArgumentException("备忘录不能为空");
        }

        this.account = memento.getAccount();
        this.password = memento.getPassword();
        this.telNo = memento.getTelNo();

        System.out.println("已恢复到状态：" + memento.getDescription());
    }

    // === getter/setter方法 ===
    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTelNo() {
        return telNo;
    }

    public void setTelNo(String telNo) {
        this.telNo = telNo;
    }

    public void show() {
        System.out.println("Account: " + account);
        System.out.println("Password: " + password);
        System.out.println("TelNo: " + telNo);
    }

    // === 便捷方法 ===
    public UserInfo copy() {
        return new UserInfo(account, password, telNo);
    }

    @Override
    public String toString() {
        return String.format("UserInfo{account='%s', password='%s', telNo='%s'}",
                account, password, telNo);
    }
}